********************************************************************************************************
*****Tabla A.11 y A.12 balance para escuelas que responden y no responden los instrumentos valora*******
********************************************************************************************************


**************************
***Observación de clase***
**************************

clear
set more off

use  "$base_out/911_All_Collapsed.dta"

rename clavecct cct

***Traigo base de tratamientos***

merge m:1 cct using "$basein/Datos adicionales/Tratamientos_completos.dta"

***Dejo solo las observaciones que necesito***

keep if _merge==2 | _merge==3

***Traigo bases de interes***

merge 1:1 cct using "$basein/Instrumentos valora/Observacion_indices.dta", gen(_indices)

drop if _indices==2


****Elimino secundarias****
rename cct school_id
merge 1:1 school_id using "$basein/Datos adicionales/secundarias.dta", gen(_secundaria)

keep if _secundaria==3

drop if secondary==1

******************
***KEEP´ONLY PEC VS PEC PLUS******
******************

drop if state==10
drop if treatgroup2=="Control"

***Genero variable de reescucha***

gen Reescuchado=0
replace Reescuchado=1 if _indices==3

rename school_id cct

eststo clear
	label var T_profe "Number of teachers "
	label var T_estu "Number of students "
	label var f_studteach_ratio "Student-teacher ratio"
	label var mathIV "Students in math achievement L-IV (\%)" 			
	label var mathI "Students in math achievement L-I (\%)" 		 			
	label var langI "Students in language achievement L-I (\%)" 		
	label var langIV "Students in language achievement L-IV (\%)"  
	label var Marginalization_ "Marginalization"
	label var Urbanas "Urbanization"
	label var PEC_Plus "Direct training"

	

	eststo PyNP:  my_ptest_treat_vs_notreat_strata PEC_Plus mathIV mathI langIV langI Marginalization_ Urbanas T_estu T_profe f_studteach_ratio , by(Reescuchado) clus_id(cct) strat_id(estrato)
	 

	 	 esttab PyNP  using "$tables/Balance_Obseracion.tex", label replace  nolines nogaps fragment ///
	 star(* 0.1 ** 0.05 *** 0.01)  ///
	 mlabels(none) nomtitles nonumbers noobs nodep collabels(none)  ///
     cells("mu_t(fmt(%9.2fc)) mu_nt(fmt(%9.2fc)) dift(fmt(%9.2fc) star pvalue(d_pt))" "se_t(par) se_nt(par) des_t(par)") 
	
	 foreach var in mathIV mathI langIV langI Marginalization_ Urbanas T_estu T_profe f_studteach_ratio{
		reg `var'  TD1 TD2, nocons vce(cluster cct) 
		sum `var' if TD1==1 & e(sample)==1
		local tempm=string(r(N), "%9.2gc")
		file open newfile using "$tables/N_`var'_NoObs.tex", write replace
		file write newfile "`tempm'"
		file close newfile
		sum `var' if TD2==1 & e(sample)==1
		local tempm=string(r(N), "%9.2gc")
		file open newfile using "$tables/N_`var'_Obs.tex", write replace
		file write newfile "`tempm'"
		file close newfile
	
		sum `var' if (TD1==1 | TD2==1) & e(sample)==1
		local tempm=string(r(N), "%9.2gc")
		file open newfile using "$tables/N_`var'_Dif_NOvsO.tex", write replace
		file write newfile "`tempm'"
		file close newfile
		}

	eststo clear
	
	eststo: reghdfe Reescuchado PEC_Plus, vce(cluster cct)  abs(estrato)
	eststo: reghdfe Reescuchado mathIV  langIV Marginalization_ Urbanas   f_studteach_ratio, vce(cluster cct)  abs(estrato)
	eststo: reghdfe Reescuchado c.PEC_Plus##c.(mathIV langIV Marginalization_ Urbanas  f_studteach_ratio), vce(cluster cct)  abs(estrato)
	
	
	esttab using "$tables/Balance_Obseracion_DID.tex" ,  fragment se ar2  label b(a2) se(a2) nocon nonumber nomtitles nolines nogaps /// 
	replace collabels(none)  ///
	stats(N , fmt("%9.2gc") labels("No. of obs." )) star(* 0.10 ** 0.05 *** 0.01)
		
***************
***SisAT***
***************

clear
set more off

use  "$base_out/911_All_Collapsed.dta"

rename clavecct cct

***Traigo base de tratamientos***

merge m:1 cct using "$basein/Datos adicionales/Tratamientos_completos.dta"

***Dejo solo las observaciones que necesito***

keep if _merge==2 | _merge==3

***Traigo bases de interes***

merge 1:1 cct using "$basein/Instrumentos valora/Herramientas_indices.dta", gen(_indices)

drop if _indices==2

****Elimino secundarias****
rename cct school_id
merge 1:1 school_id using "$basein/Datos adicionales/secundarias.dta", gen(_secundaria)

keep if _secundaria==3

drop if secondary==1

******************
***KEEP´ONLY PEC VS PEC PLUS******
******************

drop if state==10
drop if treatgroup2=="Control"

***Genero variable de reescucha***

gen Reescuchado=0
replace Reescuchado=1 if _indices==3

rename school_id cct

eststo clear
	label var T_profe "Number of teachers "
	label var T_estu "Number of students "
	label var f_studteach_ratio "Student-teacher ratio"
	label var mathIV "Students in math achievement L-IV (\%)" 			
	label var mathI "Students in math achievement L-I (\%)" 		 			
	label var langI "Students in language achievement L-I (\%)" 		
	label var langIV "Students in language achievement L-IV (\%)"  
	label var Marginalization_ "Marginalization"
	label var Urbanas "Urbanization"
	label var PEC_Plus "Direct training"

	eststo PyNP:  my_ptest_treat_vs_notreat_strata PEC_Plus mathIV mathI langIV langI Marginalization_ Urbanas T_estu T_profe f_studteach_ratio , by(Reescuchado) clus_id(cct) strat_id(estrato)
	 

	 	 esttab PyNP  using "$tables/Balance_Herramientas.tex", label replace  nolines nogaps fragment ///
	 star(* 0.1 ** 0.05 *** 0.01)  ///
	 mlabels(none) nomtitles nonumbers noobs nodep collabels(none)  ///
     cells("mu_t(fmt(%9.2fc)) mu_nt(fmt(%9.2fc)) dift(fmt(%9.2fc) star pvalue(d_pt))" "se_t(par) se_nt(par) des_t(par)") 
	
	 foreach var in mathIV mathI langIV langI Marginalization_ Urbanas T_estu T_profe f_studteach_ratio{
		reg `var'  TD1 TD2, nocons vce(cluster cct) 
		sum `var' if TD1==1 & e(sample)==1
		local tempm=string(r(N), "%9.2gc")
		file open newfile using "$tables/N_`var'_NoHerr.tex", write replace
		file write newfile "`tempm'"
		file close newfile
		sum `var' if TD2==1 & e(sample)==1
		local tempm=string(r(N), "%9.2gc")
		file open newfile using "$tables/N_`var'_Herr.tex", write replace
		file write newfile "`tempm'"
		file close newfile
	
		sum `var' if (TD1==1 | TD2==1) & e(sample)==1
		local tempm=string(r(N), "%9.2gc")
		file open newfile using "$tables/N_`var'_Dif_NHvsH.tex", write replace
		file write newfile "`tempm'"
		file close newfile
		}
		
		
			eststo clear
	label var PEC_Plus "Direct training"
	eststo: reghdfe Reescuchado PEC_Plus, vce(cluster cct)  abs(estrato)
	eststo: reghdfe Reescuchado mathIV  langIV Marginalization_ Urbanas   f_studteach_ratio, vce(cluster cct)  abs(estrato)
	eststo: reghdfe Reescuchado c.PEC_Plus##c.(mathIV langIV Marginalization_ Urbanas  f_studteach_ratio), vce(cluster cct)  abs(estrato)
	
	
	esttab using "$tables/Balance_Herramientas_DID.tex" ,  fragment se ar2  label b(a2) se(a2) nocon nonumber nomtitles nolines nogaps /// 
	replace collabels(none)  ///
	stats(N , fmt("%9.2gc") labels("No. of obs." )) star(* 0.10 ** 0.05 *** 0.01)
		


		
***************************************************
*******************Reescuchas**********************
***************************************************


clear
set more off

use  "$base_out/911_All_Collapsed.dta"

rename clavecct cct

***Traigo base de tratamientos***

merge m:1 cct using "$basein/Datos adicionales/Tratamientos_completos.dta"

***Dejo solo las observaciones que necesito***

keep if _merge==2 | _merge==3

***Traigo bases de interes***
rename cct school_id

merge 1:1 school_id using "$basein/Datos adicionales/DWMS_missing_endline.dta", gen(_DMWS)
*drop if _indices==2


****Elimino secundarias****
*rename cct school_id
merge 1:1 school_id using "$basein/Datos adicionales/secundarias.dta", gen(_secundaria)

keep if _secundaria==3

drop if secondary==1

******************
***KEEP´ONLY PEC VS PEC PLUS******
******************

drop if state==10
drop if treatgroup2=="Control"

***Genero variable de reescucha***

gen Reescuchado=0
replace Reescuchado=1 if DMS_missing_endline==0

rename school_id cct

eststo clear
	label var T_profe "Number of teachers "
	label var T_estu "Number of students "
	label var f_studteach_ratio "Student-teacher ratio"
	label var mathIV "Students in math achievement L-IV (\%)" 			
	label var mathI "Students in math achievement L-I (\%)" 		 			
	label var langI "Students in language achievement L-I (\%)" 		
	label var langIV "Students in language achievement L-IV (\%)"  
	label var Marginalization_ "Marginalization"
	label var Urbanas "Urbanization"
	label var PEC_Plus "Direct training"

	eststo PyNP:  my_ptest_treat_vs_notreat_strata PEC_Plus mathIV mathI langIV langI Marginalization_ Urbanas T_estu T_profe f_studteach_ratio , by(Reescuchado) clus_id(cct)  strat_id(estrato)
	 

	 	 esttab PyNP  using "$tables/Balance_reescucha.tex", label replace  nolines nogaps fragment ///
	 star(* 0.1 ** 0.05 *** 0.01)  ///
	 mlabels(none) nomtitles nonumbers noobs nodep collabels(none)  ///
     cells("mu_t(fmt(%9.2fc)) mu_nt(fmt(%9.2fc)) dift(fmt(%9.2fc) star pvalue(d_pt))" "se_t(par) se_nt(par) des_t(par)") 
	
	 foreach var in mathIV mathI langIV langI Marginalization_ Urbanas T_estu T_profe f_studteach_ratio{
		reg `var'  TD1 TD2 , nocons vce(cluster cct) 
		sum `var' if TD1==1 & e(sample)==1
		local tempm=string(r(N), "%9.2gc")
		file open newfile using "$tables/N_`var'_NoRee.tex", write replace
		file write newfile "`tempm'"
		file close newfile
		sum `var' if TD2==1 & e(sample)==1
		local tempm=string(r(N), "%9.2gc")
		file open newfile using "$tables/N_`var'_Ree.tex", write replace
		file write newfile "`tempm'"
		file close newfile
	
		sum `var' if (TD1==1 | TD2==1) & e(sample)==1
		local tempm=string(r(N), "%9.2gc")
		file open newfile using "$tables/N_`var'_Dif_NRvsR.tex", write replace
		file write newfile "`tempm'"
		file close newfile
		}


	eststo clear
	
	eststo: reghdfe Reescuchado PEC_Plus, vce(cluster cct)  abs(estrato)
	eststo: reghdfe Reescuchado mathIV  langIV Marginalization_ Urbanas   f_studteach_ratio, vce(cluster cct)  abs(estrato)
	eststo: reghdfe Reescuchado c.PEC_Plus##c.(mathIV langIV Marginalization_ Urbanas  f_studteach_ratio), vce(cluster cct)  abs(estrato)
	
	
	esttab using "$tables/Balance_reescucha_DID.tex" ,  fragment se ar2  label b(a2) se(a2) nocon nonumber nomtitles nolines nogaps /// 
	replace collabels(none)  ///
	stats(N , fmt("%9.2gc") labels("No. of obs." )) star(* 0.10 ** 0.05 *** 0.01)